package neusoft.example.com.yuejian06.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.CourseDetailActivity;
import neusoft.example.com.yuejian06.Bean.Course;
import neusoft.example.com.yuejian06.R;

public class CourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //private LayoutInflater inflater;
    private Context context;
    private List<Course> list;

    public CourseListAdapter(Context context,List<Course> list){
        this.list=list;
        this.context=context;
    }

    public void setList(List<Course> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.adapter_courselist,parent,false);
        RecyclerView.ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Course bean=list.get(position);
        if(bean==null) return;
        final CourseListAdapter.ViewHolder viewHolder=(CourseListAdapter.ViewHolder) holder;

        viewHolder.course_name.setText(bean.getCourse_name());
        viewHolder.course_price.setText("￥"+bean.getCourse_price());
        viewHolder.course_intro.setText(bean.getCourse_intro());
        viewHolder.course_level.setText(bean.getCourse_level());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,CourseDetailActivity.class);
                intent.putExtra("course_id", bean.getCourse_id());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView course_name,course_price,course_intro,course_level;
        public ViewHolder(View itemView) {
            super(itemView);
            course_name=(TextView) itemView.findViewById(R.id.course_name);
            course_price=(TextView) itemView.findViewById(R.id.course_price);
            course_intro=(TextView) itemView.findViewById(R.id.course_intro);
            course_level=(TextView) itemView.findViewById(R.id.course_level);
        }
    }
}
